package com.jz.community.moduleshoppingguide.home.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.RoundCornersTransformation;
import com.jz.community.basecomm.utils.RxImageTool;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.home.bean.HomeFindBean;

import java.util.List;


public class HomeMainRecommendAdapter extends BaseQuickAdapter<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean, BaseViewHolder> {

    public HomeMainRecommendAdapter(int layoutResId, @Nullable List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean entity) {
        RelativeLayout rlHomeMainRecommend = helper.getView(R.id.rl_item_home_rec_goods);
        ImageView imageView = helper.getView(R.id.home_main_recommend_iv);
        FrameLayout fl = helper.getView(R.id.item_home_rec_goods_parent);

        WindowManager wm = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        assert wm != null;
        int screen_width = wm.getDefaultDisplay().getWidth();//获取屏幕宽度

        ViewGroup.LayoutParams lp1 = rlHomeMainRecommend.getLayoutParams();
        lp1.width = (screen_width - RxImageTool.dp2px(20)) / 2;
        rlHomeMainRecommend.setLayoutParams(lp1);

        ViewGroup.LayoutParams lp2 = fl.getLayoutParams();
        lp2.width = (screen_width - RxImageTool.dp2px(20)) / 2;
        fl.setLayoutParams(lp2);

        ViewGroup.LayoutParams lp3 = imageView.getLayoutParams();
        lp3.width = (screen_width - RxImageTool.dp2px(20)) / 2;
        lp3.height = (screen_width - RxImageTool.dp2px(20)) / 2;
        imageView.setLayoutParams(lp3);

        RoundCornersTransformation transformation = new RoundCornersTransformation(mContext,
                RxImageTool.dp2px(5), RoundCornersTransformation.CornerType.ALL);
        GlideApp.with(mContext)
                .load(entity.getImage())
                .transform(transformation)
                .placeholder(R.mipmap.image_default)
                .error(R.mipmap.image_default)
                .into(imageView);

        helper.setText(R.id.home_main_recommend_name, entity.getTitle());
        helper.setText(R.id.home_main_recommend_describe, entity.getSellingDesc());
        //原价
        TextView price_red = helper.getView(R.id.home_main_recommend_price);
        TextView price_gray = helper.getView(R.id.home_main_recommend_gray);

        if (!TextUtils.isEmpty(entity.getDiscountPrice())) {
            price_red.setText(entity.getDiscountPrice());
            RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getPrice()))).setStrikethrough().into(price_gray);
        } else {
            price_red.setText(entity.getPrice());
            price_gray.setText("");
        }
        rlHomeMainRecommend.setOnClickListener(v -> ARouter.getInstance()
                .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                .withString("url", entity.getLinks().getGoodsInfo().getHref())
                .navigation());
    }


}
